<?php
/**
 * File: view.php
 * Author: Nick Regan
 * Created: 
 * Description: view module for the Internship Listing project. 
 */

/**
 * Shows a welcome message. Called if index.php page is 1st time requested.
 * @return string with welcome message.
 */
 
function showWelcome()
{
  echo "<html>
		<head>
		<title>I2C Web App</title>
		<link rel='stylesheet' type='text/css' href='css/i2c.css'>
		</head>
		<body>

		<div id='header'><h1><img align='left' src='images/unhmlogo.gif'>&nbsp; Internship2Career Web Application</h1></div>

		<div id='wrapper'>
		";
}

/**
 * Outputs a form to get user data about internship posting.
 * $_POST keys are: "sponsorName", "sponsorCity", "sponsorState", "postingDate",
 * "internshipTitle", "internshipCategory", "isPaid", "submitButton". 
 */
function showNewForm( )
{
  echo <<<_END

<td>
<form id="form" class="rounded" method="post" action="index.php">

	<h3>Enter internship opportunity information</h3>
	<fieldset>
	<br /><div class="field">
	    <label for="sponsorName">Sponsor Name:</label>
	    <input type="text" class="input" name="sponsorName" id="sponsorName" />
	    <p class="hint">Enter the Organization's Name.</p>
	</div>
	
	<div class="field">
      <label for="internshipTitle">Title:</label>
      <input type="text" class="input" name="internshipTitle" id="internshipTitle" />
      <p class="hint">Enter the Internship Title.</p>
	</div>

	<div class="field">
	    <label for="sponsorCity">City:</label>
	    <input type="text" class="input" name="sponsorCity" id="sponsorCity" />
	    <p class="hint">Enter the City.</p>
	</div>
	<div class="field">
 	   <label for="sponsorState">State:</label>
	    <input type="text" class="input" name="sponsorState" id="sponsorState" />
	    <p class="hint">Enter the State.</p>
	</div>
	<div class="field">
	    <label for="postingDate">Date:</label>
	    <input type="text" class="input" name="postingDate" id="postingDate" placeholder="YYYY/MM/DD" />
	    <p class="hint">Enter the Date.</p>
	</div>
	<div class="field">
      <label for="internshipCategory">Category:</label>
      <input type="text" class="input" name="internshipCategory" id="internshipCategory" />
      <p class="hint">Enter the Category.</p>
    </div>
    <div class="field">
      <label for="isPaid">Paid Internship?:</label>
      <select name="isPaid">
      <option value="Yes">Yes</option>
      <option value="No">No</option></select>
      <p class="hint">Is this a Paid Internship?</p>
    </div>
    <input type="submit" name="submitButton" class="btn btn-primary" value="New" />
    </fieldset>
</form>
</td>
_END;
}

/**
 * Outputs header row for the listing that has internship postings. 
 * @return string HTML code for representing a table header.
 */
function startListing()
{
  $out =  <<<_END
 
<table id="intern" class="rounded">
<tr><th colspan="8"><h3>Open Internship Opportunities</h3></th></tr>
<tr>
<th>#</th>
<th><a href="index.php?sortBy=sponsorName" target="_self">Sponsor Name</a></th>
<th><a href="index.php?sortBy=sponsorCity" target="_self">Sponsor City</a></th>
<th><a href="index.php?sortBy=sponsorState" target="_self">Sponsor State</a></th>
<th><a href="index.php?sortBy=postingDate" target="_self">Posting Date</a></th>
<th><a href="index.php?sortBy=internshipTitle" target="_self">Internship Title</a></th>
<th><a href="index.php?sortBy=internshipCategory" target="_self">Internship Category</a></th>
<th><a href="index.php?sortBy=isPaid" target="_self">Paid</a></th>
</tr>


_END;
  return $out;
}

/* removed the opening and closing form tags from startListing
<form method="get" action="index.php">


*/


//removed opening fieldset from startListing, closing fieldset from endListing, removed cellspacing="12" from table in startListing
/**
 * Outpus HTML closing tags for table and fieldset. 
 * @return string HTML closing tags.
 */
function endListing( )
{
  $out = "</table>";
  return $out;
}

/**
 * Outputs one row with internship listing data.
 * @param unsigned integer $id
 * @param string $sponsor
 * @param string $city
 * @param string $state
 * @param string $date - formatted to represent date 'YYYY-MON-DD'
 * @param string $title 
 * @param string $category 
 * @param string $isPaid 
 * @return string HTML code for representing one table row with variable 
 * substitution for all parameter variables.
 */
function showOneListing($i, $id, $sponsor, $city, $state, $date, $title, $category, 
                        $isPaid)
{

  $out = <<<_END
<tr><td>$i</td>
<td>$sponsor</td>
<td>$city</td>
<td>$state</td>
<td>$date</td>
<td>$title</td>
<td>$category</td>
<td>$isPaid</td>
<td>
<form action="index.php" method="post">
  <input type="submit" class="btn btn-danger" value="Delete" name= "deleteButton" />
  <input type="hidden" value="$id" name="deleteID" />
</form>
</td>
</tr>
_END;

  return $out;
}

/* form from line 138

<form action="index.php" method="post">
  <input type="submit" value="Delete" name= "deleteButton" />
  <input type="hidden" value="$id" name="deleteID" />
</form>

*/

/**
 * Outputs given $errorMsg. 
 * @param string $errorMsg - has error message.
 */
function showErrorMessage($errorMsg)
{
  if ($errorMsg != "") { //echoed only if there is an error
    echo "<h3 align=\"center\">The following error has occured: $errorMsg</h3>
	     </td></table></div></body></html>";
  }
}

/**
 * Outputs HTML table with internship listing data.
 * The HTML table is obtaining from concatenating the output from 
 * startListing( ), with $listing parameter, and output from endListing( ). 
 * @param string $listing - has all the internship listing data in string form.
 */
function showInternshipListing($listing)
{
  $out = ""; //to format final student listing
  
  $out = startListing() . $listing . endListing();
  echo $out;
}
?>